##
# data_source.py - Created by Timothy Morey on 7/3/2012
#
# This file provides a ctypes wrapper for libnvn items defined in data-source.h.
#

import ctypes

from libnvn import nvn
import data_schema

# DataSourceType enum values:
DataSourceTypeUnknown   = 0
DataSourceTypeLocalFile = 1
DataSourceTypeOPeNDAP   = 2
DataSourceTypeNvnServer = 3

class DataSourceDescriptor(ctypes.Structure):
    _fields_ = [
                ('Type',      ctypes.c_int),
                ('Dataset',   ctypes.c_char * 256),
                ('Varname',   ctypes.c_char * 256),
                ('LocalPort', ctypes.c_int),
               ]

    def GetServer(self):
        return self.Dataset[0:self.Dataset.find(':')]

    def GetPort(self):
        colonpos = self.Dataset.find(':')
        slashpos = self.Dataset.find('/')
        return self.Dataset[colonpos+1:slashpos-colonpos-1]

    def GetDatasetName(self):
        slashpos = self.Dataset.find('/')
        return self.Dataset[slashpos+1:]


DataSourceGetDatasetSchema = nvn.DataSourceGetDatasetSchema
DataSourceGetDatasetSchema.restype = ctypes.c_int
DataSourceGetDatasetSchema.argtypes = [
                                       DataSourceDescriptor,
                                       ctypes.POINTER(data_schema.DataSetSchema),
                                      ]

DataSourceGetGridSchema = nvn.DataSourceGetGridSchema
DataSourceGetGridSchema.restype = ctypes.c_int
DataSourceGetGridSchema.argtypes = [
                                    DataSourceDescriptor,
                                    ctypes.POINTER(data_schema.DataGridSchema),
                                   ]